package net.sangpil.classes.file
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.filesystem.File;
	
	import net.sangpil.classes.events.FileBrowserEvent;

	[Event(name = "selected", type = "net.sangpil.classes.events.FileBrowserEvent")]
	public class FileBrowser extends EventDispatcher
	{
		public function FileBrowser(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		public function openBrowser(filterArr:Array = null):void
		{
			var file:File = new File();
			file.addEventListener(Event.SELECT, onSelected);
			
			if(filterArr == null)
			{
				//When does not exist FileFilter array
				file.browse();
			}else{
				//When exist FileFilter array
				file.browse(filterArr);
			}
		}
		
		private function onSelected(e:Event):void
		{
			var file:File = e.currentTarget as File;
			
			var fileInfo:FileInfo = new FileInfo();
			fileInfo.name = file.name;
			fileInfo.path = file.nativePath;
			fileInfo.extension = file.extension;
			
			this.dispatchEvent(new FileBrowserEvent(fileInfo, FileBrowserEvent.SELECTED));
			
			/* var stream:FileStream = new FileStream();
			stream.open(file, FileMode.READ);
			
			trace(stream.readMultiByte(stream.bytesAvailable, File.systemCharset));
			
			stream.close(); */
		} 
	}
}